home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / dispnew.c < prev    next >
C/C++ Source or Header  |  1993-10-06  |  64KB  |  2,219 lines

  1. /* Updating of data structures for redisplay.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <signal.h>
  22.  
  23. #include "config.h"
  24. #include <stdio.h>
  25. #include <ctype.h>
  26.  
  27. #ifdef WINDOWSNT
  28. #define NOMINMAX
  29. #include <windows.h>
  30. /* Disable warning about \( */
  31. #pragma warning(disable:4129)
  32. #endif /* WINDOWSNT */
  33.  
  34. #include "lisp.h"
  35. #include "termchar.h"
  36. #include "termopts.h"
  37. #include "cm.h"
  38. #include "dispextern.h"
  39. #include "buffer.h"
  40. #include "frame.h"
  41. #include "window.h"
  42. #include "commands.h"
  43. #include "disptab.h"
  44. #include "indent.h"
  45. #include "termhooks.h"
  46.  
  47. #include "systty.h"
  48. #include "systime.h"
  49.  
  50. #ifdef HAVE_X_WINDOWS
  51. #include "xterm.h"
  52. #endif    /* HAVE_X_WINDOWS */
  53.  
  54. #ifdef STDC_HEADERS
  55. #include <stdlib.h>
  56. #endif
  57. #include "dispnew_p.h"
  58. #include "dispnew_d.h"
  59. #include "term_p.h"
  60. #include "term_d.h"
  61. #include "alloca_p.h"
  62. #include "xdisp_p.h"
  63. #include "scroll_p.h"
  64. #include "window_d.h"
  65. static void free_frame_glyphs _P_((FRAME_PTR frame,
  66.                                    struct frame_glyphs *glyphs));
  67. static void remake_frame_glyphs _P_((FRAME_PTR frame));
  68. static int line_hash_code _P_((register struct frame_glyphs *m, int vpos));
  69. static unsigned int line_draw_cost _P_((struct frame_glyphs *m, int vpos));
  70. static struct frame_glyphs * make_frame_glyphs _P_((register FRAME_PTR frame,
  71.                                                     int empty));
  72. static void rotate_vector _P_((pointer vector, int size, int distance));
  73. static int count_blanks _P_((register GLYPH *r));
  74. static int count_match _P_((GLYPH *str1, GLYPH *str2));
  75. static void update_line _P_((register FRAME_PTR frame, int vpos));
  76.  
  77. #ifndef max
  78. #define max(a, b) ((a) > (b) ? (a) : (b))
  79. #endif
  80. #ifndef min
  81. #define min(a, b) ((a) < (b) ? (a) : (b))
  82. #endif
  83.  
  84. #ifndef PENDING_OUTPUT_COUNT
  85. /* Get number of chars of output now in the buffer of a stdio stream.
  86.    This ought to be built in in stdio, but it isn't.
  87.    Some s- files override this because their stdio internals differ.  */
  88. #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
  89. #endif
  90.  
  91. /* Nonzero upon entry to redisplay means do not assume anything about
  92.    current contents of actual terminal frame; clear and redraw it.  */
  93.  
  94. int frame_garbaged;
  95.  
  96. /* Nonzero means last display completed.  Zero means it was preempted. */
  97.  
  98. int display_completed;
  99.  
  100. /* Lisp variable visible-bell; enables use of screen-flash
  101.    instead of audible bell.  */
  102.  
  103. int visible_bell;
  104.  
  105. /* Invert the color of the whole frame, at a low level.  */
  106.  
  107. int inverse_video;
  108.  
  109. /* Line speed of the terminal.  */
  110.  
  111. int baud_rate;
  112.  
  113. /* nil or a symbol naming the window system under which emacs is
  114.    running ('x is the only current possibility).  */
  115.  
  116. Lisp_Object Vwindow_system;
  117.  
  118. /* Version number of X windows: 10, 11 or nil.  */
  119. Lisp_Object Vwindow_system_version;
  120.  
  121. /* Vector of glyph definitions.  Indexed by glyph number,
  122.    the contents are a string which is how to output the glyph.
  123.  
  124.    If Vglyph_table is nil, a glyph is output by using its low 8 bits
  125.    as a character code.  */
  126.  
  127. Lisp_Object Vglyph_table;
  128.  
  129. /* Display table to use for vectors that don't specify their own.  */
  130.  
  131. Lisp_Object Vstandard_display_table;
  132.  
  133. /* Nonzero means reading single-character input with prompt
  134.    so put cursor on minibuffer after the prompt.
  135.    positive means at end of text in echo area;
  136.    negative means at beginning of line.  */
  137. int cursor_in_echo_area;
  138.  
  139. /* The currently selected frame.
  140.    In a single-frame version, this variable always remains 0.  */
  141.  
  142. FRAME_PTR selected_frame;
  143.  
  144. /* A frame which is not just a minibuffer, or 0 if there are no such
  145.    frames.  This is usually the most recent such frame that was
  146.    selected.  In a single-frame version, this variable always remains 0.  */
  147. FRAME_PTR last_nonminibuf_frame;
  148.  
  149. /* In a single-frame version, the information that would otherwise
  150.    exist inside frame objects lives in the following structure instead.
  151.  
  152.    NOTE: the_only_frame is not checked for garbage collection; don't
  153.    store collectible objects in any of its fields!
  154.  
  155.    You're not/The only frame in town/...  */
  156.  
  157. #ifndef MULTI_FRAME
  158. struct frame the_only_frame;
  159. #endif
  160.  
  161. /* This is a vector, made larger whenever it isn't large enough,
  162.    which is used inside `update_frame' to hold the old contents
  163.    of the FRAME_PHYS_LINES of the frame being updated.  */
  164. struct frame_glyphs **ophys_lines;
  165. /* Length of vector currently allocated.  */
  166. int ophys_lines_length;
  167.  
  168. FILE *termscript;    /* Stdio stream being used for copy of all output.  */
  169.  
  170. struct cm Wcm;        /* Structure for info on cursor positioning */
  171.  
  172. extern short ospeed;    /* Output speed (from sg_ospeed) */
  173.  
  174. int delayed_size_change;  /* 1 means SIGWINCH happened when not safe.  */
  175.  
  176. #ifdef MULTI_FRAME
  177.  
  178. DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
  179.   "Clear frame FRAME and output again what is supposed to appear on it.")
  180.   (frame)
  181.      Lisp_Object frame;
  182. {
  183.   FRAME_PTR f;
  184.  
  185.   CHECK_LIVE_FRAME (frame, 0);
  186.   f = XFRAME (frame);
  187.   update_begin (f);
  188.   /*  set_terminal_modes (); */
  189.   clear_frame ();
  190.   clear_frame_records (f);
  191.   update_end (f);
  192.   fflush (stdout);
  193.   windows_or_buffers_changed++;
  194.   /* Mark all windows as INaccurate,
  195.      so that every window will have its redisplay done.  */
  196.   mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
  197.   f->garbaged = 0;
  198.   return Qnil;
  199. }
  200.  
  201. _VOID_
  202. redraw_frame (f)
  203.      FRAME_PTR f;
  204. {
  205.   Lisp_Object frame;
  206.   XSET (frame, Lisp_Frame, f);
  207.   Fredraw_frame (frame);
  208. }
  209.  
  210. #else
  211.  
  212. DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, "",
  213.   "Clear frame FRAME and output again what is supposed to appear on it.")
  214.   (frame)
  215.      Lisp_Object frame;
  216. {
  217.   update_begin (0);
  218.   set_terminal_modes ();
  219.   clear_frame ();
  220.   update_end (0);
  221.   fflush (stdout);
  222.   clear_frame_records (0);
  223.   windows_or_buffers_changed++;
  224.   /* Mark all windows as INaccurate,
  225.      so that every window will have its redisplay done.  */
  226.   mark_window_display_accurate (FRAME_ROOT_WINDOW (0), 0);
  227.   return Qnil;
  228. }
  229.  
  230. #endif
  231.  
  232. DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
  233.   "Clear and redisplay all visible frames.")
  234.   ()
  235. {
  236.   Lisp_Object tail, frame;
  237.  
  238.   FOR_EACH_FRAME (tail, frame)
  239.     if (FRAME_VISIBLE_P (XFRAME (frame)))
  240.       Fredraw_frame (frame);
  241.  
  242.   return Qnil;
  243. }
  244.  
  245. /* This is used when frame_garbaged is set.
  246.    Redraw the individual frames marked as garbaged.  */
  247.  
  248. void
  249. redraw_garbaged_frames ()
  250. {
  251.   Lisp_Object tail, frame;
  252.  
  253.   FOR_EACH_FRAME (tail, frame)
  254.     if (FRAME_VISIBLE_P (XFRAME (frame))
  255.     && FRAME_GARBAGED_P (XFRAME (frame)))
  256.       Fredraw_frame (frame);
  257. }
  258.  
  259.  
  260. static struct frame_glyphs *
  261. make_frame_glyphs (frame, empty)
  262.      register FRAME_PTR frame;
  263.      int empty;
  264. {
  265.   register int i;
  266.   register width = FRAME_WIDTH (frame);
  267.   register height = FRAME_HEIGHT (frame);
  268.   register struct frame_glyphs *new =
  269.     (struct frame_glyphs *) xmalloc (sizeof (struct frame_glyphs));
  270.  
  271.   SET_GLYPHS_FRAME (new, frame);
  272.   new->height = height;
  273.   new->width = width;
  274.   new->used = (int *) xmalloc (height * sizeof (int));
  275.   new->glyphs = (GLYPH **) xmalloc (height * sizeof (GLYPH *));
  276.   new->highlight = (char *) xmalloc (height * sizeof (char));
  277.   new->enable = (char *) xmalloc (height * sizeof (char));
  278.   bzero (new->enable, height * sizeof (char));
  279.   new->bufp = (int *) xmalloc (height * sizeof (int));
  280.  
  281. #ifdef HAVE_X_WINDOWS
  282.   if (FRAME_X_P (frame))
  283.     {
  284.       new->top_left_x = (short *) xmalloc (height * sizeof (short));
  285.       new->top_left_y = (short *) xmalloc (height * sizeof (short));
  286.       new->pix_width = (short *) xmalloc (height * sizeof (short));
  287.       new->pix_height = (short *) xmalloc (height * sizeof (short));
  288.       new->max_ascent = (short *) xmalloc (height * sizeof (short));
  289.     }
  290. #endif
  291.  
  292.   if (empty)
  293.     {
  294.       /* Make the buffer used by decode_mode_spec.  This buffer is also
  295.          used as temporary storage when updating the frame.  See scroll.c. */
  296.       unsigned int total_glyphs = (width + 2) * sizeof (GLYPH);
  297.  
  298.       new->total_contents = (GLYPH *) xmalloc (total_glyphs);
  299.       bzero (new->total_contents, total_glyphs);
  300.     }
  301.   else
  302.     {
  303.       unsigned int total_glyphs = height * (width + 2) * sizeof (GLYPH);
  304.  
  305.       new->total_contents = (GLYPH *) xmalloc (total_glyphs);
  306.       bzero (new->total_contents, total_glyphs);
  307.       for (i = 0; i < height; i++)
  308.     new->glyphs[i] = new->total_contents + i * (width + 2) + 1;
  309.     }
  310.  
  311.   return new;
  312. }
  313.  
  314. static void
  315. free_frame_glyphs (frame, glyphs)
  316.      FRAME_PTR frame;
  317.      struct frame_glyphs *glyphs;
  318. {
  319.   if (glyphs->total_contents)
  320.     xfree (glyphs->total_contents);
  321.  
  322.   xfree (glyphs->used);
  323.   xfree (glyphs->glyphs);
  324.   xfree (glyphs->highlight);
  325.   xfree (glyphs->enable);
  326.   xfree (glyphs->bufp);
  327.  
  328. #ifdef HAVE_X_WINDOWS
  329.   if (FRAME_X_P (frame))
  330.     {
  331.       xfree (glyphs->top_left_x);
  332.       xfree (glyphs->top_left_y);
  333.       xfree (glyphs->pix_width);
  334.       xfree (glyphs->pix_height);
  335.       xfree (glyphs->max_ascent);
  336.     }
  337. #endif
  338.  
  339.   xfree (glyphs);
  340. }
  341.  
  342. static void
  343. remake_frame_glyphs (frame)
  344.      FRAME_PTR frame;
  345. {
  346.   if (FRAME_CURRENT_GLYPHS (frame))
  347.     free_frame_glyphs (frame, FRAME_CURRENT_GLYPHS (frame));
  348.   if (FRAME_DESIRED_GLYPHS (frame))
  349.     free_frame_glyphs (frame, FRAME_DESIRED_GLYPHS (frame));
  350.   if (FRAME_TEMP_GLYPHS (frame))
  351.     free_frame_glyphs (frame, FRAME_TEMP_GLYPHS (frame));
  352.  
  353.   if (FRAME_MESSAGE_BUF (frame))
  354.     {
  355.       /* Reallocate the frame's message buffer; remember that
  356.      echo_area_glyphs may be pointing here.  */
  357.       char *old_message_buf = FRAME_MESSAGE_BUF (frame);
  358.  
  359.       FRAME_MESSAGE_BUF (frame)
  360.     = (char *) xrealloc (FRAME_MESSAGE_BUF (frame),
  361.                  FRAME_WIDTH (frame) + 1);
  362.  
  363.       if (echo_area_glyphs == old_message_buf)
  364.     echo_area_glyphs = FRAME_MESSAGE_BUF (frame);
  365.       if (previous_echo_glyphs == old_message_buf)
  366.     previous_echo_glyphs = FRAME_MESSAGE_BUF (frame);
  367.     }
  368.   else
  369.     FRAME_MESSAGE_BUF (frame)
  370.       = (char *) xmalloc (FRAME_WIDTH (frame) + 1);
  371.  
  372.   FRAME_CURRENT_GLYPHS (frame) = make_frame_glyphs (frame, 0);
  373.   FRAME_DESIRED_GLYPHS (frame) = make_frame_glyphs (frame, 0);
  374.   FRAME_TEMP_GLYPHS (frame) = make_frame_glyphs (frame, 1);
  375.   SET_FRAME_GARBAGED (frame);
  376. }
  377.  
  378. /* Return the hash code of contents of line VPOS in frame-matrix M.  */
  379.  
  380. static int
  381. line_hash_code (m, vpos)
  382.      register struct frame_glyphs *m;
  383.      int vpos;
  384. {
  385.   register GLYPH *body;
  386.   register int h = 0;
  387.  
  388.   if (!m->enable[vpos])
  389.     return 0;
  390.  
  391.   /* Give all highlighted lines the same hash code
  392.      so as to encourage scrolling to leave them in place.  */
  393.   if (m->highlight[vpos])
  394.     return -1;
  395.  
  396.   body = m->glyphs[vpos];
  397.  
  398.   if (must_write_spaces)
  399.     while (1)
  400.       {
  401.     GLYPH g = *body++;
  402.  
  403.     if (g == 0)
  404.       break;
  405.     h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g - SPACEGLYPH;
  406.       }
  407.   else
  408.     while (1)
  409.       {
  410.     GLYPH g = *body++;
  411.  
  412.     if (g == 0)
  413.       break;
  414.     h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g;
  415.       }
  416.  
  417.   if (h)
  418.     return h;
  419.   return 1;
  420. }
  421.  
  422. /* Return number of characters in line in M at vpos VPOS,
  423.    except don't count leading and trailing spaces
  424.    unless the terminal requires those to be explicitly output.  */
  425.  
  426. static unsigned int
  427. line_draw_cost (m, vpos)
  428.      struct frame_glyphs *m;
  429.      int vpos;
  430. {
  431.   register GLYPH *beg = m->glyphs[vpos];
  432.   register GLYPH *end = m->glyphs[vpos] + m->used[vpos];
  433.   register int i;
  434.   register unsigned int tlen = GLYPH_TABLE_LENGTH;
  435.   register Lisp_Object *tbase = GLYPH_TABLE_BASE;
  436.  
  437.   /* Ignore trailing and leading spaces if we can.  */
  438.   if (!must_write_spaces)
  439.     {
  440.       while ((end != beg) && (*end == SPACEGLYPH))
  441.     --end;
  442.       if (end == beg)
  443.     return (0); /* All blank line. */
  444.  
  445.       while (*beg == SPACEGLYPH)
  446.     ++beg;
  447.     }
  448.  
  449.   /* If we don't have a glyph-table, each glyph is one character,
  450.      so return the number of glyphs.  */
  451.   if (tbase == 0)
  452.     return end - beg;
  453.  
  454.   /* Otherwise, scan the glyphs and accumulate their total size in I.  */
  455.   i = 0;
  456.   while ((beg <= end) && *beg)
  457.     {
  458.       register GLYPH g = *beg++;
  459.  
  460.       if (GLYPH_SIMPLE_P (tbase, tlen, g))
  461.     i += 1;
  462.       else
  463.     i += GLYPH_LENGTH (tbase, g);
  464.     }
  465.   return i;
  466. }
  467.  
  468. /* The functions on this page are the interface from xdisp.c to redisplay.
  469.  
  470.    The only other interface into redisplay is through setting
  471.    FRAME_CURSOR_X (frame) and FRAME_CURSOR_Y (frame)
  472.    and SET_FRAME_GARBAGED (frame).  */
  473.  
  474. /* cancel_line eliminates any request to display a line at position `vpos' */
  475.  
  476. _VOID_
  477. cancel_line (vpos, frame)
  478.      int vpos;
  479.      register FRAME_PTR frame;
  480. {
  481.   FRAME_DESIRED_GLYPHS (frame)->enable[vpos] = 0;
  482. }
  483.  
  484. _VOID_
  485. clear_frame_records (frame)
  486.      register FRAME_PTR frame;
  487. {
  488.   bzero (FRAME_CURRENT_GLYPHS (frame)->enable, FRAME_HEIGHT (frame));
  489. }
  490.  
  491. /* Prepare to display on line VPOS starting at HPOS within it.  */
  492.  
  493. void
  494. get_display_line (frame, vpos, hpos)
  495.      register FRAME_PTR frame;
  496.      int vpos;
  497.      register int hpos;
  498. {
  499.   register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
  500.  
  501.   if (vpos < 0)
  502.     abort ();
  503.  
  504.   if ((desired_glyphs->enable[vpos]) && desired_glyphs->used[vpos] > hpos)
  505.     abort ();
  506.  
  507.   if (! desired_glyphs->enable[vpos])
  508.     {
  509.       desired_glyphs->used[vpos] = 0;
  510.       desired_glyphs->highlight[vpos] = 0;
  511.       desired_glyphs->enable[vpos] = 1;
  512.     }
  513.  
  514.   if (hpos > desired_glyphs->used[vpos])
  515.     {
  516.       GLYPH *g = desired_glyphs->glyphs[vpos] + desired_glyphs->used[vpos];
  517.       GLYPH *end = desired_glyphs->glyphs[vpos] + hpos;
  518.  
  519.       desired_glyphs->used[vpos] = hpos;
  520.       while (g != end)
  521.     *g++ = SPACEGLYPH;
  522.     }
  523. }
  524.  
  525. /* Like bcopy except never gets confused by overlap.  */
  526.  
  527. #ifdef STDC_HEADERS
  528. #define safe_bcopy(f, t, l) memmove(t, f, l)
  529. #else
  530. void
  531. safe_bcopy (from, to, size)
  532.      char *from, *to;
  533.      int size;
  534. {
  535.   if (size <= 0 || from == to)
  536.     return;
  537.  
  538.   /* If the source and destination don't overlap, then bcopy can
  539.      handle it.  If they do overlap, but the destination is lower in
  540.      memory than the source, we'll assume bcopy can handle that.  */
  541.   if (to < from || from + size <= to)
  542.     bcopy (from, to, size);
  543.  
  544.   /* Otherwise, we'll copy from the end.  */
  545.   else
  546.     {
  547.       register char *endf = from + size;
  548.       register char *endt = to + size;
  549.  
  550.       /* If TO - FROM is large, then we should break the copy into
  551.      nonoverlapping chunks of TO - FROM bytes each.  However, if
  552.      TO - FROM is small, then the bcopy function call overhead
  553.      makes this not worth it.  The crossover point could be about
  554.      anywhere.  Since I don't think the obvious copy loop is too
  555.      bad, I'm trying to err in its favor.  */
  556.       if (to - from < 64)
  557.     {
  558.       do
  559.         *--endt = *--endf;
  560.       while (endf != from);
  561.     }
  562.       else
  563.     {
  564.       for (;;)
  565.         {
  566.           endt -= (to - from);
  567.           endf -= (to - from);
  568.  
  569.           if (endt < to)
  570.         break;
  571.  
  572.           bcopy (endf, endt, to - from);
  573.         }
  574.  
  575.       /* If SIZE wasn't a multiple of TO - FROM, there will be a
  576.          little left over.  The amount left over is
  577.          (endt + (to - from)) - to, which is endt - from.  */
  578.       bcopy (from, to, endt - from);
  579.     }
  580.     }
  581. }     
  582. #endif
  583.  
  584. /* Rotate a vector of SIZE bytes right, by DISTANCE bytes.
  585.    DISTANCE may be negative.  */
  586.  
  587. static void
  588. rotate_vector (vector, size, distance)
  589.      pointer vector;
  590.      int size;
  591.      int distance;
  592. {
  593.   char *temp = (char *) alloca (size);
  594.  
  595.   if (distance < 0)
  596.     distance += size;
  597.  
  598.   bcopy (vector, temp + distance, size - distance);
  599.   bcopy ((char *)vector + size - distance, temp, distance);
  600.   bcopy (temp, vector, size);
  601. }
  602.  
  603. /* Scroll lines from vpos FROM up to but not including vpos END
  604.    down by AMOUNT lines (AMOUNT may be negative).
  605.    Returns nonzero if done, zero if terminal cannot scroll them.  */
  606.  
  607. int
  608. scroll_frame_lines (frame, from, end, amount)
  609.      register FRAME_PTR frame;
  610.      int from, end, amount;
  611. {
  612.   register int i;
  613.   register struct frame_glyphs *current_frame
  614.     = FRAME_CURRENT_GLYPHS (frame);
  615.  
  616.   if (!line_ins_del_ok)
  617.     return 0;
  618.  
  619.   if (amount == 0)
  620.     return 1;
  621.  
  622.   if (amount > 0)
  623.     {
  624.       update_begin (frame);
  625.       set_terminal_window (end + amount);
  626.       if (!scroll_region_ok)
  627.     ins_del_lines (end, -amount);
  628.       ins_del_lines (from, amount);
  629.       set_terminal_window (0);
  630.  
  631.       rotate_vector (current_frame->glyphs + from,
  632.              sizeof (GLYPH *) * (end + amount - from),
  633.              amount * sizeof (GLYPH *));
  634.  
  635.       safe_bcopy (current_frame->used + from,
  636.           current_frame->used + from + amount,
  637.           (end - from) * sizeof current_frame->used[0]);
  638.  
  639.       safe_bcopy (current_frame->highlight + from,
  640.           current_frame->highlight + from + amount,
  641.           (end - from) * sizeof current_frame->highlight[0]);
  642.  
  643.       safe_bcopy (current_frame->enable + from,
  644.           current_frame->enable + from + amount,
  645.           (end - from) * sizeof current_frame->enable[0]);
  646.  
  647.       /* Mark the lines made empty by scrolling as enabled, empty and
  648.      normal video.  */
  649.       bzero (current_frame->used + from,
  650.          amount * sizeof current_frame->used[0]);
  651.       bzero (current_frame->highlight + from,
  652.          amount * sizeof current_frame->highlight[0]);
  653.       for (i = from; i < from + amount; i++)
  654.     {
  655.       current_frame->glyphs[i][0] = '\0';
  656.       current_frame->enable[i] = 1;
  657.     }
  658.  
  659.       safe_bcopy (current_frame->bufp + from,
  660.           current_frame->bufp + from + amount,
  661.           (end - from) * sizeof current_frame->bufp[0]);
  662.  
  663. #ifdef HAVE_X_WINDOWS
  664.       if (FRAME_X_P (frame))
  665.     {
  666.       safe_bcopy (current_frame->top_left_x + from,
  667.               current_frame->top_left_x + from + amount,
  668.               (end - from) * sizeof current_frame->top_left_x[0]);
  669.  
  670.       safe_bcopy (current_frame->top_left_y + from,
  671.               current_frame->top_left_y + from + amount,
  672.               (end - from) * sizeof current_frame->top_left_y[0]);
  673.  
  674.       safe_bcopy (current_frame->pix_width + from,
  675.               current_frame->pix_width + from + amount,
  676.               (end - from) * sizeof current_frame->pix_width[0]);
  677.  
  678.       safe_bcopy (current_frame->pix_height + from,
  679.               current_frame->pix_height + from + amount,
  680.               (end - from) * sizeof current_frame->pix_height[0]);
  681.  
  682.       safe_bcopy (current_frame->max_ascent + from,
  683.               current_frame->max_ascent + from + amount,
  684.               (end - from) * sizeof current_frame->max_ascent[0]);
  685.     }
  686. #endif                /* HAVE_X_WINDOWS */
  687.  
  688.       update_end (frame);
  689.     }
  690.   if (amount < 0)
  691.     {
  692.       update_begin (frame);
  693.       set_terminal_window (end);
  694.       ins_del_lines (from + amount, amount);
  695.       if (!scroll_region_ok)
  696.     ins_del_lines (end + amount, -amount);
  697.       set_terminal_window (0);
  698.  
  699.       rotate_vector (current_frame->glyphs + from + amount,
  700.              sizeof (GLYPH *) * (end - from - amount),
  701.              amount * sizeof (GLYPH *));
  702.  
  703.       safe_bcopy (current_frame->used + from,
  704.           current_frame->used + from + amount,
  705.           (end - from) * sizeof current_frame->used[0]);
  706.  
  707.       safe_bcopy (current_frame->highlight + from,
  708.           current_frame->highlight + from + amount,
  709.           (end - from) * sizeof current_frame->highlight[0]);
  710.  
  711.       safe_bcopy (current_frame->enable + from,
  712.           current_frame->enable + from + amount,
  713.           (end - from) * sizeof current_frame->enable[0]);
  714.  
  715.       /* Mark the lines made empty by scrolling as enabled, empty and
  716.      normal video.  */
  717.       bzero (current_frame->used + end + amount,
  718.          - amount * sizeof current_frame->used[0]);
  719.       bzero (current_frame->highlight + end + amount,
  720.          - amount * sizeof current_frame->highlight[0]);
  721.       for (i = end + amount; i < end; i++)
  722.     {
  723.       current_frame->glyphs[i][0] = '\0';
  724.       current_frame->enable[i] = 1;
  725.     }
  726.  
  727.       safe_bcopy (current_frame->bufp + from,
  728.           current_frame->bufp + from + amount,
  729.           (end - from) * sizeof current_frame->bufp[0]);
  730.  
  731. #ifdef HAVE_X_WINDOWS
  732.       if (FRAME_X_P (frame))
  733.     {
  734.       safe_bcopy (current_frame->top_left_x + from,
  735.               current_frame->top_left_x + from + amount,
  736.               (end - from) * sizeof current_frame->top_left_x[0]);
  737.  
  738.       safe_bcopy (current_frame->top_left_y + from,
  739.               current_frame->top_left_y + from + amount,
  740.               (end - from) * sizeof current_frame->top_left_y[0]);
  741.  
  742.       safe_bcopy (current_frame->pix_width + from,
  743.               current_frame->pix_width + from + amount,
  744.               (end - from) * sizeof current_frame->pix_width[0]);
  745.  
  746.       safe_bcopy (current_frame->pix_height + from,
  747.               current_frame->pix_height + from + amount,
  748.               (end - from) * sizeof current_frame->pix_height[0]);
  749.  
  750.       safe_bcopy (current_frame->max_ascent + from,
  751.               current_frame->max_ascent + from + amount,
  752.               (end - from) * sizeof current_frame->max_ascent[0]);
  753.     }
  754. #endif                /* HAVE_X_WINDOWS */
  755.  
  756.       update_end (frame);
  757.     }
  758.   return 1;
  759. }
  760.  
  761. /* After updating a window W that isn't the full frame wide,
  762.    copy all the columns that W does not occupy
  763.    into the FRAME_DESIRED_GLYPHS (frame) from the FRAME_PHYS_GLYPHS (frame)
  764.    so that update_frame will not change those columns.  */
  765.  
  766. _VOID_
  767. preserve_other_columns (w)
  768.      struct window *w;
  769. {
  770.   register int vpos;
  771.   register struct frame_glyphs *current_frame, *desired_frame;
  772.   register FRAME_PTR frame = XFRAME (w->frame);
  773.   int start = XFASTINT (w->left);
  774.   int end = XFASTINT (w->left) + XFASTINT (w->width);
  775.   int bot = XFASTINT (w->top) + XFASTINT (w->height);
  776.  
  777.   current_frame = FRAME_CURRENT_GLYPHS (frame);
  778.   desired_frame = FRAME_DESIRED_GLYPHS (frame);
  779.  
  780.   for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
  781.     {
  782.       if (current_frame->enable[vpos] && desired_frame->enable[vpos])
  783.     {
  784.       if (start > 0)
  785.         {
  786.           int len;
  787.  
  788.           bcopy (current_frame->glyphs[vpos],
  789.              desired_frame->glyphs[vpos],
  790.              start * sizeof (current_frame->glyphs[vpos]));
  791.           len = min (start, current_frame->used[vpos]);
  792.           if (desired_frame->used[vpos] < len)
  793.         desired_frame->used[vpos] = len;
  794.         }
  795.       if (current_frame->used[vpos] > end
  796.           && desired_frame->used[vpos] < current_frame->used[vpos])
  797.         {
  798.           while (desired_frame->used[vpos] < end)
  799.         desired_frame->glyphs[vpos][desired_frame->used[vpos]++]
  800.           = SPACEGLYPH;
  801.           bcopy (current_frame->glyphs[vpos] + end,
  802.              desired_frame->glyphs[vpos] + end,
  803.              ((current_frame->used[vpos] - end)
  804.               * sizeof (current_frame->glyphs[vpos])));
  805.           desired_frame->used[vpos] = current_frame->used[vpos];
  806.         }
  807.     }
  808.     }
  809. }
  810.  
  811. #if 0
  812.  
  813. /* If window w does not need to be updated and isn't the full frame wide,
  814.  copy all the columns that w does occupy
  815.  into the FRAME_DESIRED_LINES (frame) from the FRAME_PHYS_LINES (frame)
  816.  so that update_frame will not change those columns.
  817.  
  818.  Have not been able to figure out how to use this correctly.  */
  819.  
  820. preserve_my_columns (w)
  821.      struct window *w;
  822. {
  823.   register int vpos, fin;
  824.   register struct frame_glyphs *l1, *l2;
  825.   register FRAME_PTR frame = XFRAME (w->frame);
  826.   int start = XFASTINT (w->left);
  827.   int end = XFASTINT (w->left) + XFASTINT (w->width);
  828.   int bot = XFASTINT (w->top) + XFASTINT (w->height);
  829.  
  830.   for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
  831.     {
  832.       if ((l1 = FRAME_DESIRED_GLYPHS (frame)->glyphs[vpos + 1])
  833.       && (l2 = FRAME_PHYS_GLYPHS (frame)->glyphs[vpos + 1]))
  834.     {
  835.       if (l2->length > start && l1->length < l2->length)
  836.         {
  837.           fin = l2->length;
  838.           if (fin > end) fin = end;
  839.           while (l1->length < start)
  840.         l1->body[l1->length++] = ' ';
  841.           bcopy (l2->body + start, l1->body + start, fin - start);
  842.           l1->length = fin;
  843.         }
  844.     }
  845.     }
  846. }
  847.  
  848. #endif
  849.  
  850. /* On discovering that the redisplay for a window was no good,
  851.    cancel the columns of that window, so that when the window is
  852.    displayed over again get_display_line will not complain.  */
  853.  
  854. _VOID_
  855. cancel_my_columns (w)
  856.      struct window *w;
  857. {
  858.   register int vpos;
  859.   register struct frame_glyphs *desired_glyphs =
  860.     FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
  861.   register int start = XFASTINT (w->left);
  862.   register int bot = XFASTINT (w->top) + XFASTINT (w->height);
  863.  
  864.   for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
  865.     if (desired_glyphs->enable[vpos]
  866.     && desired_glyphs->used[vpos] >= start)
  867.       desired_glyphs->used[vpos] = start;
  868. }
  869.  
  870. /* These functions try to perform directly and immediately on the frame
  871.    the necessary output for one change in the buffer.
  872.    They may return 0 meaning nothing was done if anything is difficult,
  873.    or 1 meaning the output was performed properly.
  874.    They assume that the frame was up to date before the buffer
  875.    change being displayed.  They make various other assumptions too;
  876.    see command_loop_1 where these are called.  */
  877.  
  878. int
  879. direct_output_for_insert (g)
  880.      int g;
  881. {
  882.   register FRAME_PTR frame = selected_frame;
  883.   register struct frame_glyphs *current_frame
  884.     = FRAME_CURRENT_GLYPHS (frame);
  885.  
  886. #ifndef COMPILER_REGISTER_BUG
  887.   register
  888. #endif /* COMPILER_REGISTER_BUG */
  889.     struct window *w = XWINDOW (selected_window);
  890. #ifndef COMPILER_REGISTER_BUG
  891.   register
  892. #endif /* COMPILER_REGISTER_BUG */
  893.     int hpos = FRAME_CURSOR_X (frame);
  894. #ifndef COMPILER_REGISTER_BUG
  895.   register
  896. #endif /* COMPILER_REGISTER_BUG */
  897.     int vpos = FRAME_CURSOR_Y (frame);
  898.  
  899.   /* Give up if about to continue line.  */
  900.   if (hpos >= XFASTINT (w->left) + window_internal_width (w) - 1
  901.     
  902.   /* Avoid losing if cursor is in invisible text off left margin */
  903.       || (XINT (w->hscroll) && hpos == XFASTINT (w->left))
  904.     
  905.   /* Give up if cursor outside window (in minibuf, probably) */
  906.       || cursor_in_echo_area
  907.       || FRAME_CURSOR_Y (frame) < XFASTINT (w->top)
  908.       || FRAME_CURSOR_Y (frame) >= XFASTINT (w->top) + XFASTINT (w->height)
  909.  
  910.   /* Give up if cursor not really at FRAME_CURSOR_X, FRAME_CURSOR_Y */
  911.       || !display_completed
  912.  
  913.   /* Give up if buffer appears in two places.  */
  914.       || buffer_shared > 1
  915.  
  916.   /* Give up if w is minibuffer and a message is being displayed there */
  917.       || (MINI_WINDOW_P (w) && echo_area_glyphs))
  918.     return 0;
  919.  
  920.   {
  921. #ifdef HAVE_X_WINDOWS
  922.     int dummy;
  923.     int face = compute_char_face (frame, w, point - 1, -1, -1, &dummy);
  924. #else
  925.     int face = 0;
  926. #endif
  927.                   
  928.     current_frame->glyphs[vpos][hpos] = MAKE_GLYPH (g, face);
  929.   }
  930.   unchanged_modified = MODIFF;
  931.   beg_unchanged = GPT - BEG;
  932.   XFASTINT (w->last_point) = point;
  933.   XFASTINT (w->last_point_x) = hpos;
  934.   XFASTINT (w->last_modified) = MODIFF;
  935.  
  936.   reassert_line_highlight (0, vpos);
  937.   write_glyphs (¤t_frame->glyphs[vpos][hpos], 1);
  938.   fflush (stdout);
  939.   ++FRAME_CURSOR_X (frame);
  940.   if (hpos == current_frame->used[vpos])
  941.     {
  942.       current_frame->used[vpos] = hpos + 1;
  943.       current_frame->glyphs[vpos][hpos + 1] = 0;
  944.     }
  945.  
  946.   return 1;
  947. }
  948.  
  949. int
  950. direct_output_forward_char (n)
  951.      int n;
  952. {
  953.   register FRAME_PTR frame = selected_frame;
  954.   register struct window *w = XWINDOW (selected_window);
  955.  
  956.   /* Avoid losing if cursor is in invisible text off left margin
  957.      or about to go off either side of window.  */
  958.   if ((FRAME_CURSOR_X (frame) == XFASTINT (w->left)
  959.        && (XINT (w->hscroll) || n < 0))
  960.       || (n > 0
  961.       && (FRAME_CURSOR_X (frame) + 1 >= window_internal_width (w) - 1))
  962.       || cursor_in_echo_area)
  963.     return 0;
  964.  
  965.   /* Can't use direct output if highlighting a region.  */
  966.   if (!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
  967.     return 0;
  968.  
  969.   FRAME_CURSOR_X (frame) += n;
  970.   XFASTINT (w->last_point_x) = FRAME_CURSOR_X (frame);
  971.   XFASTINT (w->last_point) = point;
  972.   cursor_to (FRAME_CURSOR_Y (frame), FRAME_CURSOR_X (frame));
  973.   fflush (stdout);
  974.   return 1;
  975. }
  976.  
  977.  
  978. /* Update frame F based on the data in FRAME_DESIRED_GLYPHS.
  979.    Value is nonzero if redisplay stopped due to pending input.
  980.    FORCE nonzero means do not stop for pending input.  */
  981.  
  982. int
  983. update_frame (f, force, inhibit_hairy_id)
  984.      FRAME_PTR f;
  985.      int force;
  986.      int inhibit_hairy_id;
  987. {
  988.   register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (f);
  989.   register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (f);
  990.   register int i;
  991.   int pause;
  992.   int preempt_count = baud_rate / 2400 + 1;
  993.   extern input_pending;
  994. #ifdef HAVE_X_WINDOWS
  995.   register int downto, leftmost;
  996. #endif
  997.  
  998.   if (preempt_count <= 0)
  999.     preempt_count = 1;
  1000.  
  1001.   if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
  1002.  
  1003.   detect_input_pending ();
  1004.   if (input_pending && !force)
  1005.     {
  1006.       pause = 1;
  1007.       goto do_pause;
  1008.     }
  1009.  
  1010.   update_begin (f);
  1011.  
  1012.   if (!line_ins_del_ok)
  1013.     inhibit_hairy_id = 1;
  1014.  
  1015.   /* See if any of the desired lines are enabled; don't compute for
  1016.      i/d line if just want cursor motion. */
  1017.   for (i = 0; i < FRAME_HEIGHT (f); i++)
  1018.     if (desired_frame->enable[i])
  1019.       break;
  1020.  
  1021.   /* Try doing i/d line, if not yet inhibited.  */
  1022.   if (!inhibit_hairy_id && i < FRAME_HEIGHT (f))
  1023.     force |= scrolling (f);
  1024.  
  1025.   /* Update the individual lines as needed.  Do bottom line first.  */
  1026.  
  1027.   if (desired_frame->enable[FRAME_HEIGHT (f) - 1])
  1028.     update_line (f, FRAME_HEIGHT (f) - 1);
  1029.  
  1030. #ifdef HAVE_X_WINDOWS
  1031.   if (FRAME_X_P (f))
  1032.     {
  1033.       leftmost = downto = f->display.x->internal_border_width;
  1034.       if (desired_frame->enable[0])
  1035.     {
  1036.       current_frame->top_left_x[FRAME_HEIGHT (f) - 1] = leftmost;
  1037.       current_frame->top_left_y[FRAME_HEIGHT (f) - 1]
  1038.         = PIXEL_HEIGHT (f) - f->display.x->internal_border_width
  1039.           - current_frame->pix_height[FRAME_HEIGHT (f) - 1];
  1040.       current_frame->top_left_x[0] = leftmost;
  1041.       current_frame->top_left_y[0] = downto;
  1042.     }
  1043.     }
  1044. #endif /* HAVE_X_WINDOWS */
  1045.  
  1046.   /* Now update the rest of the lines. */
  1047.   for (i = 0; i < FRAME_HEIGHT (f) - 1 && (force || !input_pending); i++)
  1048.     {
  1049.       if (desired_frame->enable[i])
  1050.     {
  1051.       if (FRAME_TERMCAP_P (f))
  1052.         {
  1053.           /* Flush out every so many lines.
  1054.          Also flush out if likely to have more than 1k buffered
  1055.          otherwise.   I'm told that some telnet connections get
  1056.          really screwed by more than 1k output at once.  */
  1057.           int outq = PENDING_OUTPUT_COUNT (stdout);
  1058.           if (outq > 900
  1059.           || (outq > 20 && ((i - 1) % preempt_count == 0)))
  1060.         {
  1061.           fflush (stdout);
  1062.           if (preempt_count == 1)
  1063.             {
  1064. #ifdef EMACS_OUTQSIZE
  1065.               if (EMACS_OUTQSIZE (0, &outq) < 0)
  1066.             /* Probably not a tty.  Ignore the error and reset
  1067.              * the outq count. */
  1068.             outq = PENDING_OUTPUT_COUNT (stdout);
  1069. #endif
  1070.               outq *= 10;
  1071.               if (baud_rate > 0)
  1072.             sleep (outq / baud_rate);
  1073.             }
  1074.         }
  1075.           if ((i - 1) % preempt_count == 0)
  1076.         detect_input_pending ();
  1077.         }
  1078.  
  1079.       update_line (f, i);
  1080. #ifdef HAVE_X_WINDOWS
  1081.       if (FRAME_X_P (f))
  1082.         {
  1083.           current_frame->top_left_y[i] = downto;
  1084.           current_frame->top_left_x[i] = leftmost;
  1085.         }
  1086. #endif /* HAVE_X_WINDOWS */
  1087.     }
  1088.  
  1089. #ifdef HAVE_X_WINDOWS
  1090.       if (FRAME_X_P (f))
  1091.     downto += current_frame->pix_height[i];
  1092. #endif
  1093.     }
  1094.   pause = (i < FRAME_HEIGHT (f) - 1) ? i : 0;
  1095.  
  1096.   /* Now just clean up termcap drivers and set cursor, etc.  */
  1097.   if (!pause)
  1098.     {
  1099.       if (cursor_in_echo_area
  1100.       && FRAME_HAS_MINIBUF_P (f))
  1101.     {
  1102.       int top = XINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
  1103.       int row, col;
  1104.  
  1105.       if (cursor_in_echo_area < 0)
  1106.         {
  1107.           row = top;
  1108.           col = 0;
  1109.         }
  1110.       else
  1111.         {
  1112.           /* If the minibuffer is several lines high, find the last
  1113.          line that has any text on it.  */
  1114.           row = FRAME_HEIGHT (f);
  1115.           do 
  1116.         {
  1117.           row--;
  1118.           if (current_frame->enable[row])
  1119.             col = current_frame->used[row];
  1120.           else
  1121.             col = 0;
  1122.         }
  1123.           while (row > top && col == 0);
  1124.  
  1125.           if (col >= FRAME_WIDTH (f))
  1126.         {
  1127.           col = 0;
  1128.           if (row < FRAME_HEIGHT (f) - 1)
  1129.             row++;
  1130.         }
  1131.         }
  1132.  
  1133.       cursor_to (row, col);
  1134.     }
  1135.       else
  1136.     cursor_to (FRAME_CURSOR_Y (f), max (min (FRAME_CURSOR_X (f),
  1137.                           FRAME_WIDTH (f) - 1), 0));
  1138.     }
  1139.  
  1140.   update_end (f);
  1141.  
  1142.   if (termscript)
  1143.     fflush (termscript);
  1144.   fflush (stdout);
  1145.  
  1146.   /* Here if output is preempted because input is detected.  */
  1147.  do_pause:
  1148.  
  1149.   if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
  1150.   display_completed = !pause;
  1151.  
  1152.   bzero (desired_frame->enable, FRAME_HEIGHT (f));
  1153.   return pause;
  1154. }
  1155.  
  1156. /* Called when about to quit, to check for doing so
  1157.    at an improper time.  */
  1158.  
  1159. void
  1160. quit_error_check ()
  1161. {
  1162.   if (FRAME_DESIRED_GLYPHS (selected_frame) == 0)
  1163.     return;
  1164.   if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[0])
  1165.     abort ();
  1166.   if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[FRAME_HEIGHT (selected_frame) - 1])
  1167.     abort ();
  1168. }
  1169.  
  1170. /* Decide what insert/delete line to do, and do it */
  1171.  
  1172. int
  1173. scrolling (frame)
  1174.      FRAME_PTR frame;
  1175. {
  1176.   int unchanged_at_top, unchanged_at_bottom;
  1177.   int window_size;
  1178.   int changed_lines;
  1179.   int *old_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
  1180.   int *new_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
  1181.   int *draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
  1182.   register int i;
  1183.   int free_at_end_vpos = FRAME_HEIGHT (frame);
  1184.   register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (frame);
  1185.   register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (frame);
  1186.  
  1187.   /* Compute hash codes of all the lines.
  1188.      Also calculate number of changed lines,
  1189.      number of unchanged lines at the beginning,
  1190.      and number of unchanged lines at the end.  */
  1191.  
  1192.   changed_lines = 0;
  1193.   unchanged_at_top = 0;
  1194.   unchanged_at_bottom = FRAME_HEIGHT (frame);
  1195.   for (i = 0; i < FRAME_HEIGHT (frame); i++)
  1196.     {
  1197.       /* Give up on this scrolling if some old lines are not enabled.  */
  1198.       if (!current_frame->enable[i])
  1199.     return 0;
  1200.       old_hash[i] = line_hash_code (current_frame, i);
  1201.       if (! desired_frame->enable[i])
  1202.     new_hash[i] = old_hash[i];
  1203.       else
  1204.     new_hash[i] = line_hash_code (desired_frame, i);
  1205.  
  1206.       if (old_hash[i] != new_hash[i])
  1207.     {
  1208.       changed_lines++;
  1209.       unchanged_at_bottom = FRAME_HEIGHT (frame) - i - 1;
  1210.     }
  1211.       else if (i == unchanged_at_top)
  1212.     unchanged_at_top++;
  1213.       draw_cost[i] = line_draw_cost (desired_frame, i);
  1214.     }
  1215.  
  1216.   /* If changed lines are few, don't allow preemption, don't scroll.  */
  1217.   if (changed_lines < baud_rate / 2400
  1218.       || unchanged_at_bottom == FRAME_HEIGHT (frame))
  1219.     return 1;
  1220.  
  1221.   window_size = (FRAME_HEIGHT (frame) - unchanged_at_top
  1222.          - unchanged_at_bottom);
  1223.  
  1224.   if (scroll_region_ok)
  1225.     free_at_end_vpos -= unchanged_at_bottom;
  1226.   else if (memory_below_frame)
  1227.     free_at_end_vpos = -1;
  1228.  
  1229.   /* If large window, fast terminal and few lines in common between
  1230.      current frame and desired frame, don't bother with i/d calc. */
  1231.   if (window_size >= 18 && baud_rate > 2400
  1232.       && (window_size >=
  1233.       10 * scrolling_max_lines_saved (unchanged_at_top,
  1234.                       FRAME_HEIGHT (frame) - unchanged_at_bottom,
  1235.                       old_hash, new_hash, draw_cost)))
  1236.     return 0;
  1237.  
  1238.   scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
  1239.            draw_cost + unchanged_at_top - 1,
  1240.            old_hash + unchanged_at_top - 1,
  1241.            new_hash + unchanged_at_top - 1,
  1242.            free_at_end_vpos - unchanged_at_top);
  1243.  
  1244.   return 0;
  1245. }
  1246.  
  1247. /* Return the offset in its buffer of the character at location col, line
  1248.    in the given window.  */
  1249. int
  1250. buffer_posn_from_coords (window, col, line)
  1251.      struct window *window;
  1252.      int col, line;
  1253. {
  1254.   int window_left = XFASTINT (window->left);
  1255.  
  1256.   /* The actual width of the window is window->width less one for the
  1257.      DISP_CONTINUE_GLYPH, and less one if it's not the rightmost
  1258.      window.  */
  1259.   int window_width = window_internal_width (window) - 1;
  1260.  
  1261.   int startp = marker_position (window->start);
  1262.  
  1263.   /* Since compute_motion will only operate on the current buffer,
  1264.      we need to save the old one and restore it when we're done.  */
  1265.   struct buffer *old_current_buffer = current_buffer;
  1266.   struct position *posn;
  1267.  
  1268.   current_buffer = XBUFFER (window->buffer);
  1269.  
  1270.   /* It would be nice if we could use FRAME_CURRENT_GLYPHS (XFRAME
  1271.      (window->frame))->bufp to avoid scanning from the very top of
  1272.      the window, but it isn't maintained correctly, and I'm not even
  1273.      sure I will keep it.  */
  1274.   posn = compute_motion (startp, 0,
  1275.              (window == XWINDOW (minibuf_window) && startp == 1
  1276.               ? minibuf_prompt_width : 0),
  1277.              ZV, line, col,
  1278.              window_width, XINT (window->hscroll), 0);
  1279.  
  1280.   current_buffer = old_current_buffer;
  1281.  
  1282.   /* compute_motion considers frame points past the end of a line
  1283.      to be *after* the newline, i.e. at the start of the next line.
  1284.      This is reasonable, but not really what we want.  So if the
  1285.      result is on a line below LINE, back it up one character.  */
  1286.   if (posn->vpos > line)
  1287.     return posn->bufpos - 1;
  1288.   else
  1289.     return posn->bufpos;
  1290. }
  1291.  
  1292. static int
  1293. count_blanks (r)
  1294.      register GLYPH *r;
  1295. {
  1296.   register GLYPH *p = r;
  1297.   while (*p++ == SPACEGLYPH);
  1298.   return p - r - 1;
  1299. }
  1300.  
  1301. static int
  1302. count_match (str1, str2)
  1303.      GLYPH *str1, *str2;
  1304. {
  1305.   register GLYPH *p1 = str1;
  1306.   register GLYPH *p2 = str2;
  1307.  
  1308.   while ((*p1++ == *p2++) && *p1 && *p2);
  1309.   return p1 - str1 - 1;
  1310. }
  1311.  
  1312. /* Char insertion/deletion cost vector, from term.c */
  1313. extern int *char_ins_del_vector;
  1314.  
  1315. #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_WIDTH((f))])
  1316.  
  1317. static void
  1318. update_line (frame, vpos)
  1319.      register FRAME_PTR frame;
  1320.      int vpos;
  1321. {
  1322.   register GLYPH *obody, *nbody, *op1, *op2, *np1, *temp;
  1323.   int tem;
  1324.   int osp, nsp, begmatch, endmatch, olen, nlen;
  1325.   int save;
  1326.   register struct frame_glyphs *current_frame
  1327.     = FRAME_CURRENT_GLYPHS (frame);
  1328.   register struct frame_glyphs *desired_frame
  1329.     = FRAME_DESIRED_GLYPHS (frame);
  1330.  
  1331.   if (desired_frame->highlight[vpos]
  1332.       != (current_frame->enable[vpos] && current_frame->highlight[vpos]))
  1333.     {
  1334.       change_line_highlight (desired_frame->highlight[vpos], vpos,
  1335.                  (current_frame->enable[vpos] ?
  1336.                   current_frame->used[vpos] : 0));
  1337.       current_frame->enable[vpos] = 0;
  1338.     }
  1339.   else
  1340.     reassert_line_highlight (desired_frame->highlight[vpos], vpos);
  1341.  
  1342.   if (! current_frame->enable[vpos])
  1343.     {
  1344.       olen = 0;
  1345.     }
  1346.   else
  1347.     {
  1348.       obody = current_frame->glyphs[vpos];
  1349.       olen = current_frame->used[vpos];
  1350.       if (! current_frame->highlight[vpos])
  1351.     {
  1352.       if (!must_write_spaces)
  1353.         while (obody[olen - 1] == SPACEGLYPH && olen > 0)
  1354.           olen--;
  1355.     }
  1356.       else
  1357.     {
  1358.       /* For an inverse-video line, remember we gave it
  1359.          spaces all the way to the frame edge
  1360.          so that the reverse video extends all the way across.  */
  1361.  
  1362.       while (olen < FRAME_WIDTH (frame) - 1)
  1363.         obody[olen++] = SPACEGLYPH;
  1364.     }
  1365.     }
  1366.  
  1367.   /* One way or another, this will enable the line being updated.  */
  1368.   current_frame->enable[vpos] = 1;
  1369.   current_frame->used[vpos] = desired_frame->used[vpos];
  1370.   current_frame->highlight[vpos] = desired_frame->highlight[vpos];
  1371.   current_frame->bufp[vpos] = desired_frame->bufp[vpos];
  1372.  
  1373. #ifdef HAVE_X_WINDOWS
  1374.   if (FRAME_X_P (frame))
  1375.     {
  1376.       current_frame->pix_width[vpos]
  1377.     = current_frame->used[vpos]
  1378.       * FONT_WIDTH (frame->display.x->font);
  1379.       current_frame->pix_height[vpos]
  1380.     = FONT_HEIGHT (frame->display.x->font);
  1381.     }
  1382. #endif /* HAVE_X_WINDOWS */
  1383.  
  1384.   if (!desired_frame->enable[vpos])
  1385.     {
  1386.       nlen = 0;
  1387.       goto just_erase;
  1388.     }
  1389.  
  1390.   nbody = desired_frame->glyphs[vpos];
  1391.   nlen = desired_frame->used[vpos];
  1392.  
  1393.   /* Pretend trailing spaces are not there at all,
  1394.      unless for one reason or another we must write all spaces.  */
  1395.   if (! desired_frame->highlight[vpos])
  1396.     {
  1397.       if (!must_write_spaces)
  1398.     /* We know that the previous character byte contains 0.  */
  1399.     while (nbody[nlen - 1] == SPACEGLYPH)
  1400.       nlen--;
  1401.     }
  1402.   else
  1403.     {
  1404.       /* For an inverse-video line, give it extra trailing spaces
  1405.      all the way to the frame edge
  1406.      so that the reverse video extends all the way across.  */
  1407.  
  1408.       while (nlen < FRAME_WIDTH (frame) - 1)
  1409.     nbody[nlen++] = SPACEGLYPH;
  1410.     }
  1411.  
  1412.   /* If there's no i/d char, quickly do the best we can without it.  */
  1413.   if (!char_ins_del_ok)
  1414.     {
  1415.       int i,j;
  1416.  
  1417.       for (i = 0; i < nlen; i++)
  1418.     {
  1419.       if (i >= olen || nbody[i] != obody[i])    /* A non-matching char. */
  1420.         {
  1421.           cursor_to (vpos, i);
  1422.           for (j = 1; (i + j < nlen &&
  1423.                (i + j >= olen || nbody[i+j] != obody[i+j]));
  1424.            j++);
  1425.  
  1426.           /* Output this run of non-matching chars.  */ 
  1427.           write_glyphs (nbody + i, j);
  1428.           i += j - 1;
  1429.  
  1430.           /* Now find the next non-match.  */
  1431.         }
  1432.     }
  1433.  
  1434.       /* Clear the rest of the line, or the non-clear part of it.  */
  1435.       if (olen > nlen)
  1436.     {
  1437.       cursor_to (vpos, nlen);
  1438.       clear_end_of_line (olen);
  1439.     }
  1440.  
  1441.       /* Exchange contents between current_frame and new_frame.  */
  1442.       temp = desired_frame->glyphs[vpos];
  1443.       desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
  1444.       current_frame->glyphs[vpos] = temp;
  1445.  
  1446.       return;
  1447.     }
  1448.  
  1449.   if (!olen)
  1450.     {
  1451.       nsp = (must_write_spaces || desired_frame->highlight[vpos])
  1452.           ? 0 : count_blanks (nbody);
  1453.       if (nlen > nsp)
  1454.     {
  1455.       cursor_to (vpos, nsp);
  1456.       write_glyphs (nbody + nsp, nlen - nsp);
  1457.     }
  1458.  
  1459.       /* Exchange contents between current_frame and new_frame.  */
  1460.       temp = desired_frame->glyphs[vpos];
  1461.       desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
  1462.       current_frame->glyphs[vpos] = temp;
  1463.  
  1464.       return;
  1465.     }
  1466.  
  1467.   obody[olen] = 1;
  1468.   save = nbody[nlen];
  1469.   nbody[nlen] = 0;
  1470.  
  1471.   /* Compute number of leading blanks in old and new contents.  */
  1472.   osp = count_blanks (obody);
  1473.   if (!desired_frame->highlight[vpos])
  1474.     nsp = count_blanks (nbody);
  1475.   else
  1476.     nsp = 0;
  1477.  
  1478.   /* Compute number of matching chars starting with first nonblank.  */
  1479.   begmatch = count_match (obody + osp, nbody + nsp);
  1480.  
  1481.   /* Spaces in new match implicit space past the end of old.  */
  1482.   /* A bug causing this to be a no-op was fixed in 18.29.  */
  1483.   if (!must_write_spaces && osp + begmatch == olen)
  1484.     {
  1485.       np1 = nbody + nsp;
  1486.       while (np1[begmatch] == SPACEGLYPH)
  1487.     begmatch++;
  1488.     }
  1489.  
  1490.   /* Avoid doing insert/delete char
  1491.      just cause number of leading spaces differs
  1492.      when the following text does not match. */
  1493.   if (begmatch == 0 && osp != nsp)
  1494.     osp = nsp = min (osp, nsp);
  1495.  
  1496.   /* Find matching characters at end of line */
  1497.   op1 = obody + olen;
  1498.   np1 = nbody + nlen;
  1499.   op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
  1500.   while (op1 > op2 && op1[-1] == np1[-1])
  1501.     {
  1502.       op1--;
  1503.       np1--;
  1504.     }
  1505.   endmatch = obody + olen - op1;
  1506.  
  1507.   /* Put correct value back in nbody[nlen].
  1508.      This is important because direct_output_for_insert
  1509.      can write into the line at a later point.
  1510.      If this screws up the zero at the end of the line, re-establish it.  */
  1511.   nbody[nlen] = save;
  1512.   obody[olen] = 0;
  1513.  
  1514.   /* tem gets the distance to insert or delete.
  1515.      endmatch is how many characters we save by doing so.
  1516.      Is it worth it?  */
  1517.  
  1518.   tem = (nlen - nsp) - (olen - osp);
  1519.   if (endmatch && tem
  1520.       && (!char_ins_del_ok || endmatch <= char_ins_del_cost (frame)[tem]))
  1521.     endmatch = 0;
  1522.  
  1523.   /* nsp - osp is the distance to insert or delete.
  1524.      If that is nonzero, begmatch is known to be nonzero also.
  1525.      begmatch + endmatch is how much we save by doing the ins/del.
  1526.      Is it worth it?  */
  1527.  
  1528.   if (nsp != osp
  1529.       && (!char_ins_del_ok
  1530.       || begmatch + endmatch <= char_ins_del_cost (frame)[nsp - osp]))
  1531.     {
  1532.       begmatch = 0;
  1533.       endmatch = 0;
  1534.       osp = nsp = min (osp, nsp);
  1535.     }
  1536.  
  1537.   /* Now go through the line, inserting, writing and
  1538.      deleting as appropriate.  */
  1539.  
  1540.   if (osp > nsp)
  1541.     {
  1542.       cursor_to (vpos, nsp);
  1543.       delete_glyphs (osp - nsp);
  1544.     }
  1545.   else if (nsp > osp)
  1546.     {
  1547.       /* If going to delete chars later in line
  1548.      and insert earlier in the line,
  1549.      must delete first to avoid losing data in the insert */
  1550.       if (endmatch && nlen < olen + nsp - osp)
  1551.     {
  1552.       cursor_to (vpos, nlen - endmatch + osp - nsp);
  1553.       delete_glyphs (olen + nsp - osp - nlen);
  1554.       olen = nlen - (nsp - osp);
  1555.     }
  1556.       cursor_to (vpos, osp);
  1557.       insert_glyphs (NULL, nsp - osp);
  1558.     }
  1559.   olen += nsp - osp;
  1560.  
  1561.   tem = nsp + begmatch + endmatch;
  1562.   if (nlen != tem || olen != tem)
  1563.     {
  1564.       cursor_to (vpos, nsp + begmatch);
  1565.       if (!endmatch || nlen == olen)
  1566.     {
  1567.       /* If new text being written reaches right margin,
  1568.          there is no need to do clear-to-eol at the end.
  1569.          (and it would not be safe, since cursor is not
  1570.          going to be "at the margin" after the text is done) */
  1571.       if (nlen == FRAME_WIDTH (frame))
  1572.         olen = 0;
  1573.       write_glyphs (nbody + nsp + begmatch, nlen - tem);
  1574.  
  1575. #ifdef obsolete
  1576.  
  1577. /* the following code loses disastrously if tem == nlen.
  1578.    Rather than trying to fix that case, I am trying the simpler
  1579.    solution found above.  */
  1580.  
  1581.       /* If the text reaches to the right margin,
  1582.          it will lose one way or another (depending on AutoWrap)
  1583.          to clear to end of line after outputting all the text.
  1584.          So pause with one character to go and clear the line then.  */
  1585.       if (nlen == FRAME_WIDTH (frame) && fast_clear_end_of_line && olen > nlen)
  1586.         {
  1587.           /* endmatch must be zero, and tem must equal nsp + begmatch */
  1588.           write_glyphs (nbody + tem, nlen - tem - 1);
  1589.           clear_end_of_line (olen);
  1590.           olen = 0;        /* Don't let it be cleared again later */
  1591.           write_glyphs (nbody + nlen - 1, 1);
  1592.         }
  1593.       else
  1594.         write_glyphs (nbody + nsp + begmatch, nlen - tem);
  1595. #endif    /* OBSOLETE */
  1596.  
  1597.     }
  1598.       else if (nlen > olen)
  1599.     {
  1600.       write_glyphs (nbody + nsp + begmatch, olen - tem);
  1601.       insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
  1602.       olen = nlen;
  1603.     }
  1604.       else if (olen > nlen)
  1605.     {
  1606.       write_glyphs (nbody + nsp + begmatch, nlen - tem);
  1607.       delete_glyphs (olen - nlen);
  1608.       olen = nlen;
  1609.     }
  1610.     }
  1611.  
  1612.  just_erase:
  1613.   /* If any unerased characters remain after the new line, erase them.  */
  1614.   if (olen > nlen)
  1615.     {
  1616.       cursor_to (vpos, nlen);
  1617.       clear_end_of_line (olen);
  1618.     }
  1619.  
  1620.   /* Exchange contents between current_frame and new_frame.  */
  1621.   temp = desired_frame->glyphs[vpos];
  1622.   desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
  1623.   current_frame->glyphs[vpos] = temp;
  1624. }
  1625.  
  1626. DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
  1627.   1, 1, "FOpen termscript file: ",
  1628.   "Start writing all terminal output to FILE as well as the terminal.\n\
  1629. FILE = nil means just close any termscript file currently open.")
  1630.   (file)
  1631.      Lisp_Object file;
  1632. {
  1633.   if (termscript != 0) fclose (termscript);
  1634.   termscript = 0;
  1635.  
  1636.   if (! NILP (file))
  1637.     {
  1638.       file = Fexpand_file_name (file, Qnil);
  1639. #ifdef WINDOWSNT
  1640.       termscript = fopen (XSTRING (file)->data, "wb");
  1641. #else /* !WINDOWSNT */
  1642.       termscript = fopen (XSTRING (file)->data, "w");
  1643. #endif /* !WINDOWSNT */
  1644.       if (termscript == 0)
  1645.     report_file_error ("Opening termscript", Fcons (file, Qnil));
  1646.     }
  1647.   return Qnil;
  1648. }
  1649.  
  1650.  
  1651. #ifdef SIGWINCH
  1652. SIGTYPE
  1653. window_change_signal ()
  1654. {
  1655.   int width, height;
  1656.   extern int errno;
  1657.   int old_errno = errno;
  1658.  
  1659.   get_frame_size (&width, &height);
  1660.  
  1661.   /* The frame size change obviously applies to a termcap-controlled
  1662.      frame.  Find such a frame in the list, and assume it's the only
  1663.      one (since the redisplay code always writes to stdout, not a
  1664.      FILE * specified in the frame structure).  Record the new size,
  1665.      but don't reallocate the data structures now.  Let that be done
  1666.      later outside of the signal handler.  */
  1667.  
  1668.   {
  1669.     Lisp_Object tail, frame;
  1670.  
  1671.     FOR_EACH_FRAME (tail, frame)
  1672.       {
  1673.     if (FRAME_TERMCAP_P (XFRAME (frame)))
  1674.       {
  1675.         change_frame_size (XFRAME (frame), height, width, 0, 1);
  1676.         break;
  1677.       }
  1678.       }
  1679.   }
  1680.  
  1681.   signal (SIGWINCH, window_change_signal);
  1682.   errno = old_errno;
  1683. }
  1684. #endif /* SIGWINCH */
  1685.  
  1686.  
  1687. /* Do any change in frame size that was requested by a signal.  */
  1688.  
  1689. _VOID_
  1690. do_pending_window_change ()
  1691. {
  1692.   /* If window_change_signal should have run before, run it now.  */
  1693.   while (delayed_size_change)
  1694.     {
  1695.       Lisp_Object tail, frame;
  1696.  
  1697.       delayed_size_change = 0;
  1698.  
  1699.       FOR_EACH_FRAME (tail, frame)
  1700.     {
  1701.       FRAME_PTR f = XFRAME (frame);
  1702.  
  1703.       int height = FRAME_NEW_HEIGHT (f);
  1704.       int width = FRAME_NEW_WIDTH (f);
  1705.  
  1706.       if (height != 0 || width != 0)
  1707.         change_frame_size (f, height, width, 0, 0);
  1708.     }
  1709.     }
  1710. }
  1711.  
  1712.  
  1713. /* Change the frame height and/or width.  Values may be given as zero to
  1714.    indicate no change is to take place. 
  1715.  
  1716.    If DELAY is non-zero, then assume we're being called from a signal
  1717.    handler, and queue the change for later - perhaps the next
  1718.    redisplay.  Since this tries to resize windows, we can't call it
  1719.    from a signal handler.  */
  1720.  
  1721. _VOID_
  1722. change_frame_size (frame, newheight, newwidth, pretend, delay)
  1723.      register FRAME_PTR frame;
  1724.      int newheight, newwidth, pretend, delay;
  1725. {
  1726.   /* If we can't deal with the change now, queue it for later.  */
  1727.   if (delay)
  1728.     {
  1729.       FRAME_NEW_HEIGHT (frame) = newheight;
  1730.       FRAME_NEW_WIDTH (frame) = newwidth;
  1731.       delayed_size_change = 1;
  1732.       return;
  1733.     }
  1734.  
  1735.   /* This size-change overrides any pending one for this frame.  */
  1736.   FRAME_NEW_HEIGHT (frame) = 0;
  1737.   FRAME_NEW_WIDTH  (frame) = 0;
  1738.  
  1739.   /* If an argument is zero, set it to the current value.  */
  1740.   newheight || (newheight = FRAME_HEIGHT (frame));
  1741.   newwidth  || (newwidth  = FRAME_WIDTH  (frame));
  1742.  
  1743.   /* Round up to the smallest acceptable size.  */
  1744.   check_frame_size (frame, &newheight, &newwidth);
  1745.  
  1746.   /* If we're not changing the frame size, quit now.  */
  1747.   if (newheight == FRAME_HEIGHT (frame)
  1748.       && newwidth == FRAME_WIDTH (frame))
  1749.     return;
  1750.  
  1751.   if (newheight != FRAME_HEIGHT (frame))
  1752.     {
  1753.       if (FRAME_HAS_MINIBUF_P (frame)
  1754.       && ! FRAME_MINIBUF_ONLY_P (frame))
  1755.     {
  1756.       /* Frame has both root and minibuffer.  */
  1757.       set_window_height (FRAME_ROOT_WINDOW (frame),
  1758.                  newheight - 1 - FRAME_MENU_BAR_LINES (frame), 0);
  1759.       XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (frame))->top)
  1760.         = newheight - 1;
  1761.       set_window_height (FRAME_MINIBUF_WINDOW (frame), 1, 0);
  1762.     }
  1763.       else
  1764.     /* Frame has just one top-level window.  */
  1765.     set_window_height (FRAME_ROOT_WINDOW (frame),
  1766.                newheight - FRAME_MENU_BAR_LINES (frame), 0);
  1767.  
  1768.       if (FRAME_TERMCAP_P (frame) && !pretend)
  1769.     FrameRows = newheight;
  1770.  
  1771. #if 0
  1772.       if (frame->output_method == output_termcap)
  1773.     {
  1774.       frame_height = newheight;
  1775.       if (!pretend)
  1776.         FrameRows = newheight;
  1777.     }
  1778. #endif
  1779.     }
  1780.  
  1781.   if (newwidth != FRAME_WIDTH (frame))
  1782.     {
  1783.       set_window_width (FRAME_ROOT_WINDOW (frame), newwidth, 0);
  1784.       if (FRAME_HAS_MINIBUF_P (frame))
  1785.     set_window_width (FRAME_MINIBUF_WINDOW (frame), newwidth, 0);
  1786.  
  1787.       if (FRAME_TERMCAP_P (frame) && !pretend)
  1788.     FrameCols = newwidth;
  1789. #if 0
  1790.       if (frame->output_method == output_termcap)
  1791.     {
  1792.       frame_width = newwidth;
  1793.       if (!pretend)
  1794.         FrameCols = newwidth;
  1795.     }
  1796. #endif
  1797.     }
  1798.  
  1799.   FRAME_HEIGHT (frame) = newheight;
  1800.   FRAME_WIDTH (frame)  = newwidth;
  1801.     
  1802.   remake_frame_glyphs (frame);
  1803.   calculate_costs (frame);
  1804. }
  1805.  
  1806. DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
  1807.   Ssend_string_to_terminal, 1, 1, 0,
  1808.   "Send STRING to the terminal without alteration.\n\
  1809. Control characters in STRING will have terminal-dependent effects.")
  1810.   (str)
  1811.      Lisp_Object str;
  1812. {
  1813.   CHECK_STRING (str, 0);
  1814.   fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, stdout);
  1815.   fflush (stdout);
  1816.   if (termscript)
  1817.     {
  1818.       fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, termscript);
  1819.       fflush (termscript);
  1820.     }
  1821.   return Qnil;
  1822. }
  1823.  
  1824. DEFUN ("ding", Fding, Sding, 0, 1, 0,
  1825.   "Beep, or flash the screen.\n\
  1826. Also, unless an argument is given,\n\
  1827. terminate any keyboard macro currently executing.")
  1828.   (arg)
  1829.   Lisp_Object arg;
  1830. {
  1831.   if (!NILP (arg))
  1832.     {
  1833.       if (noninteractive)
  1834.     putchar (07);
  1835.       else
  1836.     ring_bell ();
  1837.       fflush (stdout);
  1838.     }
  1839.   else
  1840.     bitch_at_user ();
  1841.  
  1842.   return Qnil;
  1843. }
  1844.  
  1845. _VOID_
  1846. bitch_at_user ()
  1847. {
  1848.   if (noninteractive)
  1849.     putchar (07);
  1850.   else if (!INTERACTIVE)  /* Stop executing a keyboard macro. */
  1851.     error ("Keyboard macro terminated by a command ringing the bell");
  1852.   else
  1853.     ring_bell ();
  1854.   fflush (stdout);
  1855. }
  1856.  
  1857. DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
  1858.   "Pause, without updating display, for SECONDS seconds.\n\
  1859. SECONDS may be a floating-point value, meaning that you can wait for a\n\
  1860. fraction of a second.  Optional second arg MILLISECONDS specifies an\n\
  1861. additional wait period, in milliseconds; this may be useful if your\n\
  1862. Emacs was built without floating point support.\n\
  1863. \(Not all operating systems support waiting for a fraction of a second.)")
  1864.   (seconds, milliseconds)
  1865.      Lisp_Object seconds, milliseconds;
  1866. {
  1867.   int sec, usec;
  1868.  
  1869.   if (NILP (milliseconds))
  1870.     XSET (milliseconds, Lisp_Int, 0);
  1871.   else
  1872.     CHECK_NUMBER (milliseconds, 1);
  1873.   usec = XINT (milliseconds) * 1000;
  1874.  
  1875. #ifdef LISP_FLOAT_TYPE
  1876.   {
  1877.     double duration = extract_float (seconds);
  1878.     sec = (int) duration;
  1879.     usec += (int)((duration - sec) * 1000000);
  1880.   }
  1881. #else
  1882.   CHECK_NUMBER (seconds, 0);
  1883.   sec = XINT (seconds);
  1884. #endif
  1885.  
  1886. #ifndef EMACS_HAS_USECS
  1887.   if (sec == 0 && usec != 0)
  1888.     error ("millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
  1889. #endif
  1890.  
  1891.   /* Assure that 0 <= usec < 1000000.  */
  1892.   if (usec < 0)
  1893.     {
  1894.       /* We can't rely on the rounding being correct if user is negative.  */
  1895.       if (-1000000 < usec)
  1896.     sec--, usec += 1000000;
  1897.       else
  1898.     sec -= -usec / 1000000, usec = 1000000 - (-usec % 1000000);
  1899.     }
  1900.   else
  1901.     sec += usec / 1000000, usec %= 1000000;
  1902.  
  1903.   if (sec < 0)
  1904.     return Qnil;
  1905.  
  1906.   {
  1907.     Lisp_Object zero;
  1908.  
  1909.     XFASTINT (zero) = 0;
  1910.     wait_reading_process_input (sec, usec, zero, 0);
  1911.   }
  1912.  
  1913.   /* We should always have wait_reading_process_input; we have a dummy
  1914.      implementation for systems which don't support subprocesses.  */
  1915. #if 0
  1916.   /* No wait_reading_process_input */
  1917.   immediate_quit = 1;
  1918.   QUIT;
  1919.  
  1920. #ifdef VMS
  1921.   sys_sleep (sec);
  1922. #else /* not VMS */
  1923. /* The reason this is done this way 
  1924.     (rather than defined (H_S) && defined (H_T))
  1925.    is because the VMS preprocessor doesn't grok `defined' */
  1926. #ifdef HAVE_SELECT
  1927.   EMACS_GET_TIME (end_time);
  1928.   EMACS_SET_SECS_USECS (timeout, sec, usec);
  1929.   EMACS_ADD_TIME (end_time, end_time, timeout);
  1930.  
  1931.   while (1)
  1932.     {
  1933.       EMACS_GET_TIME (timeout);
  1934.       EMACS_SUB_TIME (timeout, end_time, timeout);
  1935.       if (EMACS_TIME_NEG_P (timeout)
  1936.       || !select (1, 0, 0, 0, &timeout))
  1937.     break;
  1938.     }
  1939. #else /* not HAVE_SELECT */
  1940.   sleep (sec);
  1941. #endif /* HAVE_SELECT */
  1942. #endif /* not VMS */
  1943.   
  1944.   immediate_quit = 0;
  1945. #endif /* no subprocesses */
  1946.  
  1947.   return Qnil;
  1948. }
  1949.  
  1950. /* This is just like wait_reading_process_input, except that
  1951.    it does the redisplay.
  1952.  
  1953.    It's also just like Fsit_for, except that it can be used for
  1954.    waiting for input as well.  */
  1955.  
  1956. Lisp_Object
  1957. sit_for (sec, usec, reading, display)
  1958.      int sec, usec, reading, display;
  1959. {
  1960.   Lisp_Object read_kbd;
  1961.  
  1962.   if (detect_input_pending ())
  1963.     return Qnil;
  1964.  
  1965.   if (display)
  1966.     redisplay_preserve_echo_area ();
  1967.  
  1968.   if (sec == 0 && usec == 0)
  1969.     return Qt;
  1970.  
  1971. #ifdef SIGIO
  1972.   gobble_input (0);
  1973. #endif
  1974.  
  1975.   XSET (read_kbd, Lisp_Int, reading ? -1 : 1);
  1976.   wait_reading_process_input (sec, usec, read_kbd, display);
  1977.  
  1978.  
  1979.   /* wait_reading_process_input should always be available now; it is
  1980.      simulated in a simple way on systems that don't support
  1981.      subprocesses.  */
  1982. #if 0
  1983.   /* No wait_reading_process_input available.  */
  1984.   immediate_quit = 1;
  1985.   QUIT;
  1986.  
  1987.   waitchannels = 1;
  1988. #ifdef VMS
  1989.   input_wait_timeout (XINT (arg));
  1990. #else                /* not VMS */
  1991. #ifndef HAVE_TIMEVAL
  1992.   timeout_sec = sec;
  1993.   select (1, &waitchannels, 0, 0, &timeout_sec);
  1994. #else /* HAVE_TIMEVAL */
  1995.   timeout.tv_sec = sec;  
  1996.   timeout.tv_usec = usec;
  1997.   select (1, &waitchannels, 0, 0, &timeout);
  1998. #endif /* HAVE_TIMEVAL */
  1999. #endif /* not VMS */
  2000.  
  2001.   immediate_quit = 0;
  2002. #endif 
  2003.  
  2004.   return detect_input_pending () ? Qnil : Qt;
  2005. }
  2006.  
  2007. DEFUN ("sit-for", Fsit_for, Ssit_for, 1, 3, 0,
  2008.   "Perform redisplay, then wait for SECONDS seconds or until input is available.\n\
  2009. SECONDS may be a floating-point value, meaning that you can wait for a\n\
  2010. fraction of a second.  Optional second arg MILLISECONDS specifies an\n\
  2011. additional wait period, in milliseconds; this may be useful if your\n\
  2012. Emacs was built without floating point support.\n\
  2013. \(Not all operating systems support waiting for a fraction of a second.)\n\
  2014. Optional third arg non-nil means don't redisplay, just wait for input.\n\
  2015. Redisplay is preempted as always if input arrives, and does not happen\n\
  2016. if input is available before it starts.\n\
  2017. Value is t if waited the full time with no input arriving.")
  2018.   (seconds, milliseconds, nodisp)
  2019.      Lisp_Object seconds, milliseconds, nodisp;
  2020. {
  2021.   int sec, usec;
  2022.  
  2023.   if (NILP (milliseconds))
  2024.     XSET (milliseconds, Lisp_Int, 0);
  2025.   else
  2026.     CHECK_NUMBER (milliseconds, 1);
  2027.   usec = XINT (milliseconds) * 1000;
  2028.  
  2029. #ifdef LISP_FLOAT_TYPE
  2030.   {
  2031.     double duration = extract_float (seconds);
  2032.     sec = (int) duration;
  2033.     usec += (int)((duration - sec) * 1000000);
  2034.   }
  2035. #else
  2036.   CHECK_NUMBER (seconds, 0);
  2037.   sec = XINT (seconds);
  2038. #endif
  2039.  
  2040. #ifndef EMACS_HAS_USECS
  2041.   if (usec != 0 && sec == 0)
  2042.     error ("millisecond `sit-for' not supported on %s", SYSTEM_TYPE);
  2043. #endif
  2044.  
  2045.   return sit_for (sec, usec, 0, NILP (nodisp));
  2046. }
  2047.  
  2048. char *terminal_type;
  2049.  
  2050. /* Initialization done when Emacs fork is started, before doing stty. */
  2051. /* Determine terminal type and set terminal_driver */
  2052. /* Then invoke its decoding routine to set up variables
  2053.   in the terminal package */
  2054.  
  2055. _VOID_
  2056. init_display ()
  2057. {
  2058. #ifdef HAVE_X_WINDOWS
  2059.   extern int display_arg;
  2060. #endif
  2061.  
  2062.   meta_key = 0;
  2063.   inverse_video = 0;
  2064.   cursor_in_echo_area = 0;
  2065.   terminal_type = (char *) 0;
  2066.  
  2067.   /* Now is the time to initialize this; it's used by init_sys_modes
  2068.      during startup.  */
  2069.   Vwindow_system = Qnil;
  2070.  
  2071.   /* If the user wants to use a window system, we shouldn't bother
  2072.      initializing the terminal.  This is especially important when the
  2073.      terminal is so dumb that emacs gives up before and doesn't bother
  2074.      using the window system.
  2075.  
  2076.      If the DISPLAY environment variable is set, try to use X, and die
  2077.      with an error message if that doesn't work.  */
  2078.  
  2079. #ifdef HAVE_X_WINDOWS
  2080.   if (! display_arg)
  2081.     {
  2082. #ifdef VMS
  2083.       display_arg = (getenv ("DECW$DISPLAY") != 0);
  2084. #else
  2085.       display_arg = (getenv ("DISPLAY") != 0);
  2086. #endif
  2087.     }
  2088.  
  2089.   if (!inhibit_window_system && display_arg)
  2090.     {
  2091.       Vwindow_system = intern ("x");
  2092. #ifdef HAVE_X11
  2093.       Vwindow_system_version = make_number (11);
  2094. #else
  2095.       Vwindow_system_version = make_number (10);
  2096. #endif
  2097.       return;
  2098.     }
  2099. #endif /* HAVE_X_WINDOWS */
  2100.  
  2101. #ifndef WINDOWSNT
  2102.   /* If no window system has been specified, try to use the terminal.  */
  2103.   if (! isatty (0))
  2104.     {
  2105.       fprintf (stderr, "emacs: standard input is not a tty\n");
  2106.       exit (1);
  2107.     }
  2108.  
  2109.   /* Look at the TERM variable */
  2110.   terminal_type = (char *) getenv ("TERM");
  2111.   if (!terminal_type)
  2112.     {
  2113. #ifdef VMS
  2114.       fprintf (stderr, "Please specify your terminal type.\n\
  2115. For types defined in VMS, use  set term /device=TYPE.\n\
  2116. For types not defined in VMS, use  define emacs_term \"TYPE\".\n\
  2117. \(The quotation marks are necessary since terminal types are lower case.)\n");
  2118. #else
  2119.       fprintf (stderr, "Please set the environment variable TERM; see tset(1).\n");
  2120. #endif
  2121.       exit (1);
  2122.     }
  2123. #endif /* !WINDOWSNT */
  2124.  
  2125. #ifdef VMS
  2126.   /* VMS DCL tends to upcase things, so downcase term type.
  2127.      Hardly any uppercase letters in terminal types; should be none.  */
  2128.   {
  2129.     char *new = (char *) xmalloc (strlen (terminal_type) + 1);
  2130.     char *p;
  2131.  
  2132.     strcpy (new, terminal_type);
  2133.  
  2134.     for (p = new; *p; p++)
  2135.       if (isupper (*p))
  2136.     *p = tolower (*p);
  2137.  
  2138.     terminal_type = new;
  2139.   }    
  2140. #endif
  2141.  
  2142.   term_init (terminal_type);
  2143.  
  2144.   remake_frame_glyphs (selected_frame);
  2145.   calculate_costs (selected_frame);
  2146.  
  2147.   /* X and Y coordinates of the cursor between updates. */
  2148.   FRAME_CURSOR_X (selected_frame) = 0;
  2149.   FRAME_CURSOR_Y (selected_frame) = 0;
  2150.  
  2151. #ifdef SIGWINCH
  2152. #ifndef CANNOT_DUMP
  2153.   if (initialized)
  2154. #endif /* CANNOT_DUMP */
  2155.     signal (SIGWINCH, window_change_signal);
  2156. #endif /* SIGWINCH */
  2157. }
  2158.  
  2159. _VOID_
  2160. syms_of_display ()
  2161. {
  2162. #ifdef MULTI_FRAME
  2163.   defsubr (&Sredraw_frame);
  2164. #endif
  2165.   defsubr (&Sredraw_display);
  2166.   defsubr (&Sopen_termscript);
  2167.   defsubr (&Sding);
  2168.   defsubr (&Ssit_for);
  2169.   defsubr (&Ssleep_for);
  2170.   defsubr (&Ssend_string_to_terminal);
  2171.  
  2172.   DEFVAR_INT ("baud-rate", &baud_rate,
  2173.     "The output baud rate of the terminal.\n\
  2174. On most systems, changing this value will affect the amount of padding\n\
  2175. and the other strategic decisions made during redisplay.");
  2176.   DEFVAR_BOOL ("inverse-video", &inverse_video,
  2177.     "*Non-nil means invert the entire frame display.\n\
  2178. This means everything is in inverse video which otherwise would not be.");
  2179.   DEFVAR_BOOL ("visible-bell", &visible_bell,
  2180.     "*Non-nil means try to flash the frame to represent a bell.");
  2181.   DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
  2182.     "*Non-nil means no need to redraw entire frame after suspending.\n\
  2183. A non-nil value is useful if the terminal can automatically preserve\n\
  2184. Emacs's frame display when you reenter Emacs.\n\
  2185. It is up to you to set this variable if your terminal can do that.");
  2186.   DEFVAR_LISP ("window-system", &Vwindow_system,
  2187.     "A symbol naming the window-system under which Emacs is running\n\
  2188. \(such as `x'), or nil if emacs is running on an ordinary terminal.");
  2189.   DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
  2190.     "The version number of the window system in use.\n\
  2191. For X windows, this is 10 or 11.");
  2192.   DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
  2193.     "Non-nil means put cursor in minibuffer, at end of any message there.");
  2194.   DEFVAR_LISP ("glyph-table", &Vglyph_table,
  2195.     "Table defining how to output a glyph code to the frame.\n\
  2196. If not nil, this is a vector indexed by glyph code to define the glyph.\n\
  2197. Each element can be:\n\
  2198.  integer: a glyph code which this glyph is an alias for.\n\
  2199.  string: output this glyph using that string (not impl. in X windows).\n\
  2200.  nil: this glyph mod 256 is char code to output,\n\
  2201.     and this glyph / 256 is face code for X windows (see `x-set-face').");
  2202.   Vglyph_table = Qnil;
  2203.  
  2204.   DEFVAR_LISP ("standard-display-table", &Vstandard_display_table,
  2205.     "Display table to use for buffers that specify none.\n\
  2206. See `buffer-display-table' for more information.");
  2207.   Vstandard_display_table = Qnil;
  2208.  
  2209.   /* Initialize `window-system', unless init_display already decided it.  */
  2210. #ifdef CANNOT_DUMP
  2211.   if (noninteractive)
  2212. #endif
  2213.     {
  2214.       Vwindow_system = Qnil;
  2215.       Vwindow_system_version = Qnil;
  2216.     }
  2217. }
  2218.  
  2219.